/* 
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */


import { EntityRepository, Repository } from "typeorm";
import { MemberEntity } from "../entity/MemberEntity";

@EntityRepository(MemberEntity)
export class MemberRepository extends Repository<MemberEntity> {
	
	
	queryAll() {
		return this.find({
            order : {
                position : "ASC",
                code : "ASC"
            }
        });
	}

    public queryByValid(valid : boolean) {
        return this.find({
            where: {
                valid : valid
            }
        });
    }

    public queryByValidWithoutWinner(valid : boolean, bingo : string) {
        return this.createQueryBuilder("member")
                .where("member.valid = :_valid AND member.id NOT IN (SELECT w_member FROM lottery.l_winner WHERE w_bingo = :_bingo)",
                    { _valid: valid, _bingo: bingo })
                .addOrderBy("member.position", "ASC")
                .addOrderBy("member.code", "ASC")
            .getMany();
    }

    public queryByValidWithoutWinnerAndBlack (valid : boolean, bingo : string, prize : number) {
        return this.createQueryBuilder("member")
                .where("member.valid = :_valid" +
                    " AND member.id NOT IN (SELECT w_member FROM lottery.l_winner WHERE w_bingo = :_bingo)" +
                    " AND member.id NOT IN (SELECT b_member FROM lottery.l_black WHERE b_bingo = :_bingo AND b_prize = :_prize) ",
                    { _valid: valid, _bingo: bingo, _prize : prize })
                .addOrderBy("member.position", "ASC")
                .addOrderBy("member.code", "ASC")
            .getMany();
    }


}